package com.tony.blog.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tony.blog.api.utils.PageUtil;
import com.tony.blog.api.utils.QueryUtil;
import com.tony.blog.model.entity.SysRoleEntity;
import com.tony.blog.model.mapper.SysRoleMapper;
import com.tony.blog.server.service.SysRoleDeptService;
import com.tony.blog.server.service.SysRoleMenuService;
import com.tony.blog.server.service.SysRoleService;
import com.tony.blog.server.service.SysUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysRoleServiceImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/25/9:55
 * @Description: 角色
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {

    @Autowired
    SysRoleMenuService sysRoleMenuService;
    @Autowired
    SysRoleDeptService sysRoleDeptService;
    @Autowired
    SysUserRoleService sysUserRoleService;

    /**
    * @Description: queryPage 获取角色列表信息(分页)
    * @Param: [paramMap]
    * @return: com.tony.pmp.common.utils.PageUtil
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 10:00
    */
    @Override
    public PageUtil queryPage(Map<String, Object> paramMap) {
        String search = (paramMap.get("search") != null)? (String) paramMap.get("search") : "";

        IPage<SysRoleEntity> iPage = new QueryUtil<SysRoleEntity>().getQueryPage(paramMap);
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<SysRoleEntity>()
                .like(StringUtils.isNotBlank(search),"role_name",search);
        IPage<SysRoleEntity> resPage = this.page(iPage,wrapper);
        return new PageUtil(resPage);
    }

    /**
    * @Description: saveRole 新增角色
    * @Param: [role]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 10:47
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRole(SysRoleEntity role) {
        role.setCreateTime(DateTime.now().toDate());
        if (!this.save(role)) {
            return false;
        }
        //新增角色菜单关联信息
        if (!sysRoleMenuService.saveOrUpdate(role.getRoleId(),role.getMenuIdList())) {
            return false;
        }

        //新增角色部门关联信息
        if (!sysRoleDeptService.saveOrUpdate(role.getRoleId(),role.getDeptIdList())) {
            return false;
        }
        return true;
    }

    /**
    * @Description: updateRole 更新角色信息
    * @Param: [role]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 15:34
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRole(SysRoleEntity role) {
        if (!this.updateById(role)) {
            return false;
        }
        //更新角色菜单信息
        if (!sysRoleMenuService.saveOrUpdate(role.getRoleId(),role.getMenuIdList())) {
            return false;
        }
        //更新角色部门信息
        if (!sysRoleDeptService.saveOrUpdate(role.getRoleId(),role.getDeptIdList())) {
            return false;
        }
        return true;
    }

    /**
    * @Description: deleBatch 批量删除角色信息
    * @Param: [ids]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 16:25
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleBatch(Long[] ids) {
        List<Long> roleIds = Arrays.asList(ids);
        this.removeByIds(roleIds);
        //删除角色菜单数据
        sysRoleMenuService.deleteBatch(roleIds);
        //删除角色部门数据
        sysRoleDeptService.deleteBatch(roleIds);
        //删除角色用户数据
        sysUserRoleService.deleteBatch(roleIds);
        return true;
    }
}
